/**
 * Created by hasee on 2018/7/31.
 */
//阻止冒泡
// $('.msg-content').click(function () {
//     event.stopPropagation();
// })

$(document).on('click','.dialog',function () {
    closeDialog();
})
$('.notice').click(function(){
    $(this).css('visibility','hidden');
})
//显示提示框
function showDialog(msg,content='') {
    div = '<div class="dialog">';
    div += '<div class="dialog-box">';
    div += '<span class="dialog-close">×</span>';
    div += '<p class="dialog-text">'+msg+'</p>';
    div += '<div class="dialog-content">'+content;
    div += '</div>';
    div += '</div>';
    div += '</div>';
    $('html').append(div);
}
//关闭提示框
function closeDialog() {
    $('.dialog').remove();
}

//显示分享提示
function showNotice() {
    $('.notice').css('visibility', 'visible');
}
//隐藏分享提示
function hideNotice() {
    $('.msg .notice').css('visibility', 'hidden');
}

//wx分享碎片给朋友
function sharePieceToFriend(title, desc, img_url, share_id, piece_id, product_id, callback) {
    showNotice();
    wx.ready(function () {
        wx.onMenuShareAppMessage({
            title: title,
            desc: desc,
            link: 'http://' + document.domain + "/product/index/product_id/" + product_id + '/share_id/' + share_id,
            imgUrl: img_url,
            type: 'link',
            success: function (data) {
                callback(data);
                //隐藏分享引导
                hideNotice();
                closeDialog();
            },
            complete: function () {
                showDialog('分享成功');
            }
        });
    })
}

//wx分享碎片到朋友圈
function sharePieceToTimeLine(title, img_url, share_id, piece_id, product_id, callback) {
    showNotice();
    wx.ready(function () {
        wx.onMenuShareTimeline({
            title: title,
            link: 'http://' + document.domain + "/product/index/product_id/" + product_id + '/share_id/' + share_id,
            imgUrl: img_url,
            trigger: function (res) {
                //alert('用户点击分享到朋友圈');
            },
            success: function (data) {
                callback(data);
                //隐藏分享引导
                hideNotice();
                closeDialog();
            },
            cancel: function (res) {
                //alert('已取消');
            },
            fail: function (res) {
                alert(res.errMsg);
            }
        });
    })
}
//wx分享活动页面
function shareProductToFriend(title, desc, img_url, product_id, page, callback) {
    wx.ready(function () {
        wx.onMenuShareAppMessage({
            title: title,
            desc: desc,
            link: 'http://' + document.domain + "/product/index/product_id/" + product_id,
            // imgUrl: 'http://img5.imgtn.bdimg.com/it/u=2098134936,3730401387&fm=27&gp=0.jpg',
            imgUrl: img_url,
            type: 'link',
            success: function (data) {
                //添加分享页面记录
                $.get('/share_page/createOne?page=' + page + '&product_id=' + product_id, function (data) {
                    console.log(data);
                })
                callback(data)
            }
        });
    })
}

//分享到朋友圈
function shareProductToTimeLine(title, img_url, product_id, page, callback) {
    wx.ready(function () {
        // 2.2 监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口
        wx.onMenuShareTimeline({
            title: title,
            link: 'http://' + document.domain + "/product/index/product_id/" + product_id,
            imgUrl: img_url,
            trigger: function (res) {
                //alert('用户点击分享到朋友圈');
            },
            success: function (res) {
                // alert('您已获得抽奖机会，赶紧去赢大奖吧～～');
                alert('分享成功！');
                //分享之后增加游戏次数
            },
            cancel: function (res) {
                //alert('已取消');
            },
            fail: function (res) {
                alert(res.errMsg);
            }
        });
    })
}
//普通分享给朋友（全局默认）
function shareGameToFriend(title, desc, img_url, callback) {
    wx.ready(function () {
        wx.onMenuShareAppMessage({
            title: title,
            desc: desc,
            link: '/',
            imgUrl: img_url,
            type: 'link',
            success: function (data) {
                //添加分享页面记录
                $.get('/share_page/createOne?page=home', function (data) {
                    console.log(data);
                })
                callback(data)
            }
        });
    })
}
//普通分享到朋友圈（全局默认）
function shareGameToTimeLine(title, img_url, callback) {
    wx.ready(function () {
        wx.onMenuShareTimeline({
            title: title,
            link: '/',
            imgUrl: img_url,
            trigger: function (res) {
                //alert('用户点击分享到朋友圈');
            },
            success: function (data) {
                //添加分享页面记录
                $.get('/share_page/createOne?page=home', function (data) {
                    console.log(data);
                })
                callback(data)
            }
        });
    })
}

//变更碎片数量
function changeNumber(piece_id, num) {
    var node = $('.piece-id-' + piece_id);
    var number = node.html();
    number = parseInt(number) + num;
    node.html(number);
    if (number > 0) {
        node.parents('.piece-item').find('.dark-mask').css('visibility', 'hidden');
    } else {
        node.parents('.piece-item').find('.dark-mask').css('visibility', 'visible');
    }
}
